#include "timerbtn.h"
#include <QMouseEvent>
#include <QDebug>

TimerBtn::TimerBtn(QWidget *parent)
:QPushButton(parent)
,_counter(WEAT_TIME)
{
    _timer = new QTimer(this);

    connect(_timer, &QTimer::timeout, [this](){
        --_counter;
        if(_counter <= 0)
        {
            _timer->stop();
            _counter = WEAT_TIME;
            this->setText("获取");
            this->setEnabled(true);
            return;
        }
        this->setText(QString::number(_counter));
    });
}
TimerBtn::~TimerBtn()
{
    _timer->stop();
}

void TimerBtn::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        this->setEnabled(false);
        this->setText(QString::number(_counter));
        _timer->start(1000);
        emit clicked(); //继承自 QPushButton，当按钮被点击时发送信号
    }
    // 通过调研基类的函数确保事件正常执行（如点击效果）
    QPushButton::mouseReleaseEvent(e);
}
